package com.sunran;

import com.sunran.model.FileBean;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;

import javax.print.attribute.standard.Media;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Root resource (exposed at "myresource" path)
 */
@Path("/files")
public class FileResource {

    private static  List<File> tmpAllFiles = new ArrayList<>();
    public static final String saveFilePath = "./FileUpload";

    static {
        File file = new File(saveFilePath);
        if(!file.exists()){
            file.mkdirs();
        }
    }
    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.APPLICATION_JSON)
    public FileBean upload(@FormDataParam("path") String dirParam,@FormDataParam("file")InputStream inputStream, @FormDataParam("file")FormDataContentDisposition formDataContentDisposition) throws IOException {

        System.out.println(dirParam);

        FileBean fileBean = new FileBean();
        fileBean.setFileName(formDataContentDisposition.getFileName());
        fileBean.setFileSize(inputStream.available());
        //创建文件夹
        File file = new File(saveFilePath+dirParam);
        if(!file.exists()){
            file.mkdirs();
        }
        //创建文件
        file = new File(saveFilePath+dirParam+fileBean.getFileName());
        if(!file.exists()){
            file.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream(file);

        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(bytes))!=-1){
            fos.write(bytes,0,len);
        }
        fos.flush();
        fos.close();
        return fileBean;
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<FileBean> getFiles(){
        listAllFiles(saveFilePath);
        List<FileBean> fileBeans = new ArrayList<>();
        for(File fileItem : tmpAllFiles){
            FileBean fileBean = new FileBean();
            fileBean.setFileSize(fileItem.getTotalSpace());
            fileBean.setFileName(fileItem.getName());
            fileBean.setFileUri(fileItem.getPath());
            fileBeans.add(fileBean);
        }
        return fileBeans;
    }


    public void listAllFiles(String filePath){
        File file = new File(filePath);
        if (!file.exists()) {
            return;
        }
        tmpAllFiles.clear();
        listFiles(filePath);
    }

    public void listFiles(String path){
        File file = new File(path);

        if(!file.isDirectory()){
            tmpAllFiles.add(file);
            return;
        }
        for(File fileItem:file.listFiles()){
            listFiles(fileItem.getPath());
        }
    }

    @GET
    @Path("/file")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response download(@QueryParam("path") String path) throws UnsupportedEncodingException {
        File file = new File(path);
        System.out.println(path);
        //如果文件不存在，提示404
        if(!file.exists()){
            return Response.status(Response.Status.NOT_FOUND).build();
        }

        return Response
                .ok(file)
                .header("Content-disposition","attachment;filename=" + URLEncoder.encode(file.getName(),"utf-8"))
                .header("Cache-Control", "no-cache").build();

    }
}
